//
//  SYMomentsTypeTableViewCell.m
//  BSHOUYIFANG
//
//  Created by lanou on 15/8/13.
//  Copyright (c) 2015年 CYB. All rights reserved.
//

#import "SYMomentsTypeTableViewCell.h"
#import "Header.h"
#import <SDWebImageManager.h>
#import <UIImageView+WebCache.h>

@implementation SYMomentsTypeTableViewCell

- (instancetype)initWithStyle:(UITableViewCellStyle)style reuseIdentifier:(NSString *)reuseIdentifier{
    self = [super initWithStyle:style reuseIdentifier:reuseIdentifier];
    if (self) {
        //底层视图
        _mainView = [[UIView alloc]initWithFrame:CGRectMake(0, 0, SCREEN_WIDTH, 110)];
        _mainView.backgroundColor = [UIColor colorWithRed:247 / 255.0 green:239 / 255.0 blue:239 / 255.0 alpha:1];
        [self.contentView addSubview:_mainView];
        
        //圆角视图
        _baseView = [[UIView alloc]initWithFrame:CGRectMake(5, 0, SCREEN_WIDTH - 10, 100)];
        _baseView.backgroundColor = [UIColor whiteColor];
        _baseView.layer.cornerRadius = 5;
        //_baseView.layer.masksToBounds = YES;
        
        _baseView.layer.shadowColor = [UIColor grayColor].CGColor;
        _baseView.layer.shadowOffset = CGSizeMake(0, 1);
        _baseView.layer.shadowOpacity = 0.8;
        _baseView.layer.shadowRadius = 0.5;
        
        [_mainView addSubview:_baseView];
        
        //头像视图
        _iconView = [[UIImageView alloc]initWithFrame:CGRectMake(5, 5, _baseView.frame.size.height - 10, _baseView.frame.size.height - 10)];
        [_baseView addSubview:_iconView];
        
        //标题视图
        _titleLabel = [[UILabel alloc]initWithFrame:CGRectMake(_iconView.frame.size.width + _iconView.frame.origin.x + 5, 5, SCREEN_WIDTH - _iconView.frame.origin.x - _iconView.frame.size.width - 5 - 85, 20)];
        [_baseView addSubview:_titleLabel];
        
        //简介视图
        _introLabel = [[UILabel alloc]initWithFrame:CGRectMake(_titleLabel.frame.origin.x, _titleLabel.frame.origin.y + _titleLabel.frame.size.height + 5, SCREEN_WIDTH - _iconView.frame.size.width - _iconView.frame.origin.x - 5, 15)];
        _introLabel.textColor = [UIColor colorWithRed:150 / 255.0 green:150 / 255.0 blue:150 / 255.0 alpha:1];
        _introLabel.font = [UIFont systemFontOfSize:13];
        [_baseView addSubview:_introLabel];
        
        //粉丝视图
        _fansLabel = [[UILabel alloc]initWithFrame:CGRectMake(_titleLabel.frame.origin.x, _introLabel.frame.origin.y + _introLabel.frame.size.height + 5, _introLabel.frame.size.width, _baseView.frame.size.height - _introLabel.frame.size.height - _introLabel.frame.origin.y - 5)];
        [_baseView addSubview:_fansLabel];
        
        //新发帖数视图
        _countLabel = [[UILabel alloc]initWithFrame:CGRectMake(SCREEN_WIDTH - 80, _titleLabel.frame.origin.y, 100, 15)];
        _countLabel.textColor = [UIColor colorWithRed:150 / 255.0 green:150 / 255.0 blue:150 / 255.0 alpha:1];
        _countLabel.font = [UIFont systemFontOfSize:12];
        [_baseView addSubview:_countLabel];
        
        //粉丝头像视图
        CGFloat dia = (SCREEN_WIDTH - _iconView.frame.origin.x - _iconView.frame.size.width - 15) / 5  - 5 < _fansLabel.frame.size.height - 10 ? (SCREEN_WIDTH - _iconView.frame.origin.x - _iconView.frame.size.width - 15) / 5 - 5 : _fansLabel.frame.size.height - 10;
        
        _imageView1 = [[UIImageView alloc]initWithFrame:CGRectMake(5, 5, dia, dia)];
        _imageView1.layer.cornerRadius = dia / 2;
        _imageView1.layer.masksToBounds = YES;
        _imageView1.tag = 6000;
        [_fansLabel addSubview:_imageView1];
        
        _imageView2 = [[UIImageView alloc]initWithFrame:CGRectMake(dia + 10, 5, dia, dia)];
        _imageView2.layer.cornerRadius = dia / 2;
        _imageView2.layer.masksToBounds = YES;
        _imageView2.tag = 6001;
        [_fansLabel addSubview:_imageView2];
        
        _imageView3 = [[UIImageView alloc]initWithFrame:CGRectMake(dia * 2 + 15, 5, dia, dia)];
        _imageView3.layer.cornerRadius = dia / 2;
        _imageView3.layer.masksToBounds = YES;
        _imageView3.tag = 6002;
        [_fansLabel addSubview:_imageView3];
        
        _imageView4 = [[UIImageView alloc]initWithFrame:CGRectMake(dia * 3 + 20, 5, dia, dia)];
        _imageView4.layer.cornerRadius = dia / 2;
        _imageView4.layer.masksToBounds = YES;
        _imageView4.tag = 6003;
        [_fansLabel addSubview:_imageView4];
        
        _imageView5 = [[UIImageView alloc]initWithFrame:CGRectMake(dia  * 4 + 25, 5, dia, dia)];
        _imageView5.layer.cornerRadius = dia / 2;
        _imageView5.layer.masksToBounds = YES;
        _imageView5.tag = 6004;
        [_fansLabel addSubview:_imageView5];

        
        self.selectionStyle = UITableViewCellSelectionStyleNone;
    }
    return self;
}

- (void)setModel:(SYMomentsTypeModel *)model{
    _titleLabel.text = model.quan_name;
    SDWebImageManager *manage = [SDWebImageManager sharedManager];
    [manage downloadImageWithURL:[NSURL URLWithString:model.quan_logo] options:SDWebImageProgressiveDownload progress:^(NSInteger receivedSize, NSInteger expectedSize) {
    } completed:^(UIImage *image, NSError *error, SDImageCacheType cacheType, BOOL finished, NSURL *imageURL) {
        _iconView.image = image;
    }];
    _introLabel.text = model.quan_slogan;
    _countLabel.text = [NSString stringWithFormat:@"今日发帖:%ld",model.new_say_count];
    [self parserFans:model];
}


- (void)parserFans:(SYMomentsTypeModel *)model{
    NSArray *arr = model.quan_fans;
    for (int i = 0; i < arr.count; i++) {
        if (i == 5) {
            return;
        }
        NSDictionary *dict = arr[i];
        UIImageView *imageView = (UIImageView *)[self viewWithTag:6000 + i];
        dispatch_async(dispatch_get_global_queue(0, 0), ^{
            [imageView sd_setImageWithURL:[NSURL URLWithString:dict[@"user_face"]]];
        });
    }
}


- (void)awakeFromNib {
    // Initialization code
}

- (void)setSelected:(BOOL)selected animated:(BOOL)animated {
    [super setSelected:selected animated:animated];

    // Configure the view for the selected state
}

@end
